<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类别" prop="categoryName">
        <el-input
          v-model="queryParams.categoryName"
          placeholder="请输入类别"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="商品标题" prop="goodTitle">
        <el-input
          v-model="queryParams.goodTitle"
          placeholder="请输入商品标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入价格"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="是否删除" prop="isDelete">
        <el-input
          v-model="queryParams.isDelete"
          placeholder="请输入是否删除"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:spu:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:spu:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:spu:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:spu:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="spuList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品ID" align="center" prop="goodSpuId" />
      <el-table-column label="类别" align="center" prop="categoryName" />
      <el-table-column label="轮播图" align="center" prop="topImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.topImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="首页图片" align="center" prop="homeImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.homeImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="goodTitle" />
      <el-table-column label="价格" align="center" prop="price" />
<!--      <el-table-column label="是否删除" align="center" prop="isDelete" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:spu:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:spu:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品的基本属性对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="商品所属类别的ID" prop="goodSpuCategoryId">
          <el-input v-model="form.goodSpuCategoryId" placeholder="请输入商品所属类别的ID" />
        </el-form-item>-->

        <el-form-item label="类别" prop="goodSpuCategoryId">
          <!--          <el-input v-model="form.categoryParentId" placeholder="请输入类别的父类别" />-->
          <el-row class="block-col-2">
            <el-col :span="12">
              <el-dropdown trigger="click">
                <span class="el-dropdown-link">{{goodsCategoryChoice}}<i class="el-icon-arrow-down el-icon--right"></i></span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-for="(item,index) in categoryList"
                                    :key="index"
                                    icon="el-icon-apple"
                                    @click.native="addCategoryChoice(item)">{{ item.categoryName }}</el-dropdown-item>

                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </el-form-item>


        <el-form-item label="商品的轮播图展示图片">
          <image-upload v-model="form.topImage"/>
        </el-form-item>
        <el-form-item label="首页图片">
          <image-upload v-model="form.homeImage"/>
        </el-form-item>
        <el-form-item label="商品标题" prop="goodTitle">
          <el-input v-model="form.goodTitle" placeholder="请输入商品标题" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入首页home显示的商品价格" />
        </el-form-item>
<!--        <el-form-item label="是否删除" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入是否删除" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSpu, getSpu, delSpu, addSpu, updateSpu, listSpuAndCategoryName } from '@/api/system/spu'
import { findParentIdByName, listCategoryAndName } from '@/api/system/category'

export default {
  name: "Spu",
  data() {
    return {
      // 类别表格数据
      categoryList: [],
      //新增类别选中
      goodsCategoryChoice: '请选择类别',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品的基本属性表格数据
      spuList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodSpuCategoryId: null,
        topImage: null,
        homeImage: null,
        goodTitle: null,
        price: null,
        isDelete: null,
        categoryName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getCategoryList();
  },
  methods: {
    /** 新增父类别 下拉框选中*/
    addCategoryChoice(item) {
      this.goodsCategoryChoice = item.categoryName
      return new Promise(resolve => {
        findParentIdByName(this.goodsCategoryChoice).then(res =>{

           const categoryId = res.data[0].categoryId
          resolve(categoryId)
        })
      }).then(categoryId=>{
        /*let stringCategoryIdRes = '\'' + categoryIdRes + '\''*/
        this.form.goodSpuCategoryId = categoryId
      })
    },
    /** 查询类别的列表*/
    getCategoryList() {
      listCategoryAndName(this.queryParams).then(response => {

        this.categoryList = response.rows;
      });
    },
    /** 查询商品的基本属性列表 */
    getList() {
      this.loading = true;
      listSpuAndCategoryName(this.queryParams).then(response => {

        this.spuList = response.rows;
        this.total = response.total;

        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        goodSpuId: null,
        goodSpuCategoryId: null,
        topImage: null,
        homeImage: null,
        goodTitle: null,
        price: null,
        createTime: null,
        isDelete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.goodSpuId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品的基本属性";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const goodSpuId = row.goodSpuId || this.ids
      getSpu(goodSpuId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品的基本属性";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.goodSpuId != null) {
            updateSpu(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSpu(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const goodSpuIds = row.goodSpuId || this.ids;
      this.$modal.confirm('是否确认删除商品的基本属性编号为"' + goodSpuIds + '"的数据项？').then(function() {
        return delSpu(goodSpuIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/spu/export', {
        ...this.queryParams
      }, `spu_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
